DEMO #1: In this demo, you'll use a variable to store a number, then use that variable in a command!
A variable is a name that stores a value. In this program, we are going to use variables to store numbers that are generated randomly! A variable creation statement uses an equal sign to connect the name to the stored value.
Hover over the parts of the variable creation statement on the stage to learn more about each part.
Try clicking the person, then click Run again to see what changes on stage!
To navigate the page using the TAB key, first press ESC to exit the code editor.
sprite = codesters.Sprite("person11", 0, -60)
sprite.set_size(.8)
#Pick a random number from 1 to 360 and store it in secret_number
secret_number = random.randint(1,360)
stage.wait(2)
sprite.turn_right(secret_number)
stage.set_axis(100)
# text = codesters.Text("text", x, y, "color")
var_name = codesters.Text("secret_number", -26, 22, "orange")
# text = codesters.Text("text", x, y, "color")
equals_sign = codesters.Text("=", 8, 22, "black")
# text = codesters.Text("text", x, y, "color")
var_value = codesters.Text(str(secret_number), 22, 22, "blue")
# circ_code = codesters.Text("codesters.Circle(0, -100, circle_radius, \"gold\")")
# circ_code.set_size(.7)
title = codesters.Text("Parts of a Variable Creation Statement", 0, 70, "black")
what_is = codesters.Text(" ", 0, 55, "black")
explanation = codesters.Text(" ", 0, 42, "black")
explanation.set_size(.7)
def mouse_move():
x = stage.mouse_x()
y = stage.mouse_y()
if y < 35 and y > 7:
if x < -2.5 and x > -55:
what_is.set_color("orange")
what_is.set_text("Name")
explanation.set_text("The name is where your program stores the value.")
elif x > -2.5 and x < 12:
what_is.set_color("black")
what_is.set_text("Equal Sign")
explanation.set_text("Connects the name to the stored value.")
elif x > 12 and x < 37:
what_is.set_color("blue")
what_is.set_text("Value")
explanation.set_text("The value stored in the variable.")
else:
what_is.set_text(" ")
explanation.set_text("Hover over the parts of the variable statement.")
# elif y > -50:
# if y > -25 and y < 25:
# what_is.set_color("purple")
# what_is.set_text("Dice")
# explanation.set_text("The dice has a numbered side that is controlled by the variable dice_roll.")
else:
what_is.set_text(" ")
explanation.set_text("Hover over the parts of the variable statement.")
stage.event_mouse_move(mouse_move)
reverse = -secret_number
def click(sprite):
sprite.say("Wheeeeee!", 2)
sprite.turn_right(reverse)
sprite.say("I just spun " + str(secret_number) + " degrees!")
# add other actions...
sprite.event_click(click)
tester = TestManager()
tester.display_success_message("Great job! Be sure to hover over each part!")
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.